/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utils;

import java.io.File; // File
import java.io.IOException; // IOException

/**
 *
 * @author Agapito
 */
public class Filter {

    String imageParentPath = "";
    String imageName = "";
    
    /**
     * 
     * @param imageParentPath
     * @throws java.io.IOException
     */
    public Filter(String imageParentPath, String imageName) throws IOException{
        File infile = new File(imageParentPath);
        if(infile.exists()){
            this.imageParentPath = imageParentPath;
            this.imageName = imageName;
        }else{ 
            this.imageParentPath = null;
            this.imageName = null;
        }
    }
    
    public void redFilter() throws IOException{
        if(!imageParentPath.equalsIgnoreCase(null)){
            ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
            ImageRGB im_modified = new ImageRGB(im_original.lx,im_original.ly);
            
            // Copy only the red channel
            //Ejemplo: Copiar sólo canal rojo
            for(int i=0;i<im_original.lx;i++){
                for(int j=0;j<im_original.ly;j++){
                    im_modified.pix[i][j][0] = im_original.pix[i][j][0]; // color rojo se copia
                    im_modified.pix[i][j][1] = 0;  // color verde se deja en "negro"
                    im_modified.pix[i][j][2] = 0; // color azul se deja en "negro"
		}
            }		
		im_modified.save(imageParentPath+"/image_red.jpg", "jpg");
        }
    }
    
    public void greenFilter() throws IOException{
        if(!imageParentPath.equalsIgnoreCase(null)){
            ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
            ImageRGB im_modified = new ImageRGB(im_original.lx,im_original.ly);
            
            // Copy only the green channel
            //Ejemplo: Copiar sólo canal verde
            for(int i=0;i<im_original.lx;i++){
                for(int j=0;j<im_original.ly;j++){
                    im_modified.pix[i][j][0] = 0; // color rojo se deja en "negro"
                    im_modified.pix[i][j][1] = im_original.pix[i][j][1]; // color verde se copia
                    im_modified.pix[i][j][2] = 0; // color azul se deja en "negro"
		}
            }		
		im_modified.save(imageParentPath+"/image_green.jpg", "jpg");
        }
    }
    
    public void blueFilter() throws IOException{
        System.out.println("uno");
        if(!imageParentPath.equalsIgnoreCase(null)){
            System.out.println("dos");
            ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
            ImageRGB im_modified = new ImageRGB(im_original.lx,im_original.ly);
            
            // Copy only the blue channel
            //Ejemplo: Copiar sólo canal azul
            for(int i=0;i<im_original.lx;i++){
                for(int j=0;j<im_original.ly;j++){
                    im_modified.pix[i][j][0] = 0; // color rojo se deja en "negro"
                    im_modified.pix[i][j][1] = 0;  // color verde se deja en "negro"
                    im_modified.pix[i][j][2] = im_original.pix[i][j][2]; // color azul se copia
		}
            }		
		im_modified.save(imageParentPath+"/image_blue.jpg", "jpg");
        }
    }
    
    public void sepiaFilter() throws IOException{
        if(!imageParentPath.equalsIgnoreCase(null)){
            ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
            ImageRGB im_modified = new ImageRGB(im_original.lx,im_original.ly);
            
            int R,G,B = 0;
            double Y/*,Q*/ = 0;
            double I = 51;
            
            //Ejemplo: imagen en tono sepia
            for(int i=0;i<im_original.lx; i++){
            	for(int j=0;j<im_original.ly;j++){
                    R = im_original.pix[i][j][0];
                    G = im_original.pix[i][j][1];
                    B = im_original.pix[i][j][2];
                    
                    // Transformation
                    
                    Y = 0.299 * R + 0.587 * G + 0.114 * B;
                    
                    // Calculo de las transformaciones de imagen
                    im_modified.pix[i][j][0] = (int)(1.0 * Y + 0.956 * I /*+ 0.621 * Q*/);
                    im_modified.pix[i][j][1] = (int)(1.0 * Y - 0.272 * I /*- 0.647 * Q*/);
                    im_modified.pix[i][j][2] = (int)(1.0 * Y - 1.105 * I /*+ 1.702 * Q*/);
                    if(im_modified.pix[i][j][0]>255) im_modified.pix[i][j][0] = 255;
                    if(im_modified.pix[i][j][1]>255) im_modified.pix[i][j][1] = 255;
                    if(im_modified.pix[i][j][2]>255) im_modified.pix[i][j][2] = 255;
                    if(im_modified.pix[i][j][0]<0) im_modified.pix[i][j][0] = 0;
                    if(im_modified.pix[i][j][1]<0) im_modified.pix[i][j][1] = 0;
                    if(im_modified.pix[i][j][2]<0) im_modified.pix[i][j][2] = 0;
                }
            }
            im_modified.save(imageParentPath+"/image_sepia.jpg", "jpg");
        }
    }
    
    public void inverseFilter() throws IOException{
        if(!imageParentPath.equalsIgnoreCase(null)){
            ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
            ImageRGB im_modified = new ImageRGB(im_original.lx,im_original.ly);
            
            // Copy only the blue channel
            //Ejemplo: Copiar sólo canal azul
            for(int i=0;i<im_original.lx;i++){
                for(int j=0;j<im_original.ly;j++){
                    im_modified.pix[i][j][0] = 255 - im_original.pix[i][j][0];
                    im_modified.pix[i][j][1] = 255 - im_original.pix[i][j][1];
                    im_modified.pix[i][j][2] = 255 - im_original.pix[i][j][2];
                }
            }		
		im_modified.save(imageParentPath+"/image_inverse.jpg", "jpg");
        }
    }
    
    public void BWFilter() throws IOException{
        if(!imageParentPath.equalsIgnoreCase(null)){
            ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
            ImageBW im_modified = new ImageBW(im_original.lx,im_original.ly);
            
            for(int i=0;i<im_original.lx;i++){
                for(int j=0;j<im_original.ly;j++){
                    im_modified.pix[i][j] = ImageBW.calcGris(im_original.pix[i][j][0],
                            im_original.pix[i][j][1], im_original.pix[i][j][2]);
                }
            }		
            im_modified.save(imageParentPath+"/image_BW.jpg", "jpg");
        }
    }
    
    public void inverseBWFilter() throws IOException{
        if(!imageParentPath.equalsIgnoreCase(null)){
            ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
            ImageBW im_modified = new ImageBW(im_original.lx,im_original.ly);
            
            for(int i=0;i<im_original.lx;i++){
                for(int j=0;j<im_original.ly;j++){
                    im_modified.pix[i][j] = 255 - ImageBW.calcGris(im_original.pix[i][j][0],
                            im_original.pix[i][j][1], im_original.pix[i][j][2]);
                }
            }		
            im_modified.save(imageParentPath+"/image_inverseBW.jpg", "jpg");
        }
    }
    
    public void plusLuminosity(int lum) throws IOException{
    	if(!imageParentPath.equalsIgnoreCase(null)){
    		ImageRGB im_original = new ImageRGB(imageParentPath+"/"+imageName);
    		ImageRGB im_modified = new ImageRGB(im_original.lx,im_original.ly);
    		
    		// Copy only the blue channel
            //Ejemplo: Aumentar en 40 el tono de la imagen.
            for(int i=0;i<im_original.lx;i++){
                for(int j=0;j<im_original.ly;j++){
                    im_modified.pix[i][j][0] = im_original.pix[i][j][0]+40;
                    im_modified.pix[i][j][1] = im_original.pix[i][j][1]+40;
                    im_modified.pix[i][j][2] = im_original.pix[i][j][2]+40;
                    if(im_modified.pix[i][j][0]>255) im_modified.pix[i][j][0] = 255;
                    if(im_modified.pix[i][j][1]>255) im_modified.pix[i][j][1] = 255;
                    if(im_modified.pix[i][j][2]>255) im_modified.pix[i][j][2] = 255;
                    if(im_modified.pix[i][j][0]<0) im_modified.pix[i][j][0] = 0;
                    if(im_modified.pix[i][j][1]<0) im_modified.pix[i][j][1] = 0;
                    if(im_modified.pix[i][j][2]<0) im_modified.pix[i][j][2] = 0;
                }
            }		
            im_modified.save(imageParentPath+"/image_plus"+lum+".jpg", "jpg");
    	}
    }
    
}
